package org.yjc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.KingRoot;
import org.yjc.entity.LesseeAdmin;
import org.yjc.entity.Log;
import org.yjc.entity.SysUser;
import org.yjc.form.KingRootForm;
import org.yjc.security.UserUtils;
import org.yjc.service.KingRootService;
import org.yjc.service.LesseeAdminService;
import org.yjc.service.LesseeService;
import org.yjc.service.LogService;

@Controller
@RequestMapping(value="/kingroot")
public class KingRootController {
	@Autowired
	private LesseeAdminService lesseeadminservice;
	@Autowired
	private KingRootService kingrootservice;
	@Autowired
	private UserUtils userutils;
	@Autowired
	private LogService logservice;
	
	@RequestMapping(value="/kingrootmain")
	private String alllesseeadmin(ModelMap map) {
		List<LesseeAdmin> lesseeadmins = lesseeadminservice.findAll(); //获取到所有租户管理员
		Integer lesseeadminid = 0;
		if (!lesseeadmins.isEmpty()) {
			lesseeadminid = lesseeadmins.get(0).getId();
		}
		map.put("lesseeadmins", lesseeadmins);
		map.put("lesseeadminid", lesseeadminid);
		return "kingroot/kingrootmain";
	}
	
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Object list(JqgridParam param,Integer lesseeadminId) {
		Pageable pageable = param.buildPageable();
		Specification spec = new Specification<KingRoot>() {

			@Override
			public Predicate toPredicate(Root<KingRoot> root, CriteriaQuery<?> arg1, CriteriaBuilder cb) {
				Predicate predicate = cb.equal(root.get("lessee").get("lesseeadmin").get("id"), lesseeadminId);
				return predicate;
			}
		};
		Page<KingRoot> page;
		if (param.getSearch()) {
			spec = spec.and(param.buildSpec());
		}
		page = kingrootservice.findAll(spec, pageable);
		return JqgridUtils.getPageResult(page);
	}
	
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(KingRootForm form) {
		String oper = form.getOper();
		KingRoot kingroot = new KingRoot();
		if("del".equals(oper)) {
			SysUser user = userutils.getUser();	 //获取当前用户
			Log log = new Log();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			log.setName(user.getUsername());
			log.setOperationTime(df.format(new Date())); //获取当前系统时间
			log.setOperatiobName("删除租户");
			logservice.save(log);
			kingrootservice.deleteById(form.getId());
		}else {
			if("edit".equals(oper)) {
				kingroot = kingrootservice.findById(form.getId());
			}
			SysUser user = userutils.getUser();	 //获取当前用户
			Log log = new Log();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			log.setName(user.getUsername());
			log.setOperationTime(df.format(new Date())); //获取当前系统时间
			log.setOperatiobName("新增租户");
			logservice.save(log);
			BeanUtils.copyProperties(form, kingroot,"id");
			kingrootservice.save(kingroot);
		}
		return "OK";	
	}
}
